using System;
using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public class MiniMapService : GameService
    {
        public enum MiniMapMode
        {
            TARGET,
            FULL
        }

        public static MiniMapService single;
        private List<MiniMapItem> itemList;
        private MiniMapCamera camera;
        private MiniMap Map;
        private MiniMapMode mode = MiniMapMode.FULL;

        private UnityObject target;

        static MiniMapService()
        {
            single = new MiniMapService();
        }

        public void OnInit()
        {
            itemList = new List<MiniMapItem>();
        }

        public void OnDestroy()
        {
            single = new MiniMapService();
        }

        public void SetMap(MiniMap map)
        {
            Map = map;
        }

        public void InitSize(float x, float y)
        {
            Map.OnInit(x, y);
        }

        public void SetTarget(UnityObject obj)
        {
            target = obj;
            Map.SetTarget(obj);
        }

        public void OnUpdate()
        {
            if (Map == null) return;
            switch (mode)
            {
                case MiniMapMode.FULL:
                    if (!Map.full) mode = MiniMapMode.TARGET;
                    break;
                case MiniMapMode.TARGET:
                    if (Map.full) mode = MiniMapMode.FULL;
                    break;
            }

            if (camera != null) camera.Refresh(Map);

            foreach (var item in itemList)
            {
                if (!item.show) continue;
                if (item.element == target)
                    UpdateTarget(item);
                else
                    item.Refresh(Map);
            }

            foreach (var item in Map.itemList)
            {
                if (!item.show) continue;
                if (target != null && item.element == target)
                    UpdateTarget(item);
                else
                    item.Refresh(Map);
            }
        }

        private void UpdateTarget(MiniMapItem item)
        {
            switch (mode)
            {
                case MiniMapMode.FULL:
                    item.Refresh(Map);
                    break;
                case MiniMapMode.TARGET:
                    item.transform.position = Vector3.zero;
                    break;
            }
            item.image.transform.rotation =
                Quaternion.Euler(new Vector3(0, 180, item.element.GetRotation().eulerAngles.y));

        }

        public void CloseMiniMapItem(MiniMapItem item)
        {
            if (item == null) return;
            item.Hide();
            itemList.Remove(item);
        }

        public void OpenMiniMapItem(string type, ref MiniMapItem item, Element element)
        {
            if (item == null)
            {
                item = ObjectService.single.GetObj("Base", string.Format("MiniMap/Item/{0}", type)).GetComponent<MiniMapItem>();
                item.transform.SetParent(Map.Body.transform);
                item.transform.localScale = Vector3.one;
            }

            item.element = element;
            item.Show();
            item.Init(Map);
            itemList.Add(item);
        }

        // private void UpdateItem(MiniMapItem item)
        // {
        //     item.Refresh(Map);
        //     return;
        //     var tp = item.element.GetPosition();
        //     var p = new Vector2(tp.x, tp.z);
        //     p -= offset;
        //     p.x = Map.halfSize.x / Map.scale * p.x;
        //     p.y = Map.halfSize.y / Map.scale * p.y;
        //     var size = new Vector2();
        //     size.x = Math.Abs(p.x) > Map.halfSize.x ? Map.halfSize.x * (p.x > 0 ? 1 : -1) : p.x;
        //     size.y = Math.Abs(p.y) > Map.halfSize.y ? Map.halfSize.y * (p.y > 0 ? 1 : -1) : p.y;
        //     size.x += size.x / Map.halfSize.x * -10;
        //     size.y += size.y / Map.halfSize.y * -10;
        //     item.image.rectTransform.anchoredPosition = size;
        //     return;
        //     if (Math.Abs(p.x) < Map.halfSize.x && Math.Abs(p.y) < Map.halfSize.y)
        //     {
        //         item.image.rectTransform.anchoredPosition = p;
        //         return;
        //     }

        //     float r;
        //     var d = Vector2.Distance(Vector2.zero, p);
        //     if (p.x / p.y > Map.size.x / Map.size.y)
        //         r = Map.halfSize.x / p.x;
        //     // item.image.rectTransform.anchoredPosition = new Vector2(Map.width / 2, Math.Sqrt(Math.Pow(r * d, 2) - Math.Pow(Map.width / 2, 2)).ToFloat());
        //     else
        //         r = Map.halfSize.y / p.y;
        //     // item.image.rectTransform.anchoredPosition = new Vector2(Math.Sqrt(Math.Pow(r * d, 2) - Math.Pow(Map.height / 2, 2)).ToFloat(), Map.height / 2);
        //     Debug.LogErrorFormat("{0} {1}", r, p);
        //     item.image.rectTransform.anchoredPosition = r * d * p.normalized;
        // }

        // private void UpdateCamera(MiniMapCamera c)
        // {
        //     c.Refresh(Map);
        // }
    }
}